<template>
	<view>
		<view class="total">
			    <view class="box">
					<input v-model="keyword" class="input" type="text" placeholder="请输入关键字">
					<button class="search" type="search" >搜索</button>
				</view>
			</view>
			<!-- 轮播图-->
			<view class="home">
				<swiper indicator-dots circular autoplay>
					<swiper-item v-for="item in lunbo_img" :key="item.id">
						<image :src="item.img" ></image>
					</swiper-item>
				</swiper>
			</view>
		
			<view class="hotGoods">
				<view class="tit">母婴专区</view>
				<navigator class="nav" url="../goods/goods">查看更多>>></navigator>
				<view class="goods_list" >
					<view class="goods_item" v-for="item in filGoods" :key="item.id">
						<image :src="item.img"></image>
						<view class="price">
							<text>{{item.price}}</text>
							<text>{{item.old_price}}</text>
						</view>
						<view class="title">{{item.title}}
						<button  class="duihuan" type="default" @click="exchange(item.price)">兑换</button>
						</view>
						<view class="dec">{{item.dec}}</view>
					</view>
				</view>
		</view>
	</view>
</template>

<script>
	export default{
		data(){
			return{
				keyword:"",
                lunbo_img: [
                    {id:1,img:'https://anmti.cn/img/nimg2/12.png'},
                    {id:2,img:'https://anmti.cn/img/nimg2/11.png'},
                    {id:3,img:'https://anmti.cn/img/nimg2/13.png'},
                    {id:4,img:'https://anmti.cn/img/nimg2/14.png'}
				],
				goods:[],
				integral:0,
			}
		},
		onLoad(){this.geishop()},
		computed:{
			filGoods(){
				return this.goods.filter(p=>{
					return p.title.indexOf(this.keyword)!== -1
				})
			}
		},
		methods:{
			
			async geishop(){
			const datas	=await	uni.$http.get('/cantlogin/getGoodsdata')
			if(datas.statusCode!==200) return uni.$showMsg()
			this.goods=datas.data
				},
			
			getData: function(){
				uni.showLoading({
					title: '数据加载中'
				});
				return new Promise((resolve, reject) => {
					uni.request({
						//api地址
						url: 'https://anmti.cn/canlogin/sinInTimes',
						//设置请求类型等于外部传入类型
						method: 'get',
						//配置请求头信息
						header: {
							"Cookie": "JSESSIONID=" + uni.getStorageSync('token').JseSsionId,
						},
						//请求成功
						success: (res) => {
							console.log(res);
							if (res.statusCode !== 200) return uni.$showMsg()
							this.integral=res.data.integral
							resolve('suc');
								uni.hideLoading();
						},
					//请求失败
						fail: (err) => {
							reject('err');
							uni.$showMsg('加载失败')
							uni.hideLoading();
						},
					})
				})
			},	
			async exchange(price){
			await this.getData()
				if(price>this.integral)
				uni.$showMsg('积分不足')
				else{
				uni.$showMsg('库存紧缺，请下次再兑换')
				}
			},
		}
	}
</script>

<style lang="scss">
	@import url("./integral_mall.css");
	/* 轮播图的样式设置*/
	.home{
		position: relative;
		top: 20px;
		swiper{
			width: 750rpx;
			height:380rpx;
			image{
				width: 100%;
				height: 100%;
			}
		}
	}
	.hotGoods{
		margin-top: 60px;
		background: #eee;
		overflow: hidden;
		.tit{
			height: 50px;
			line-height: 50px;
			color: #EE6AA7;
			text-align: center;
			font-size: 18px;
			font-weight: 500;
			letter-spacing: 20px;
			background: #FFFFFF;
			margin: 7rpx 0;
		}
		.nav{
			position: relative;
			left: 270px;
		}
	  }
	  .goods_list{
	  		padding: 0 15rpx;
	  		display: flex;
	  		flex-wrap: wrap;
	  		justify-content: space-between;
	  		.goods_item{
	  			background: #FFFFFF;
	  			width: 355rpx;
	  			margin:10rpx 0;
	  			padding: 15rpx;
	  			box-sizing: border-box;
	  			image{
	  				width: 80%;
	  				height: 150px;
	  				margin: auto;
	  				display: block;
	  			}
	  		}
	  		.price{
	  			color:red;
	  			font-weight: 600;
	  			font-size: 15px;
				line-height: 60rpx;
	  			margin: 20rpx 0 5rpx 0;
	  			text:nth-child(2){
	  				color:#ccc;
	  				font-size: 15px;
	  				margin-left: 15px;
	  				text-decoration: line-through;
	  			}
	  		}	  		
	  		.title{
	  			font-size: 35rpx;
	  			font-weight: 1000;
	  			padding-bottom: 8rpx;
	  			padding-top: 5rpx;
	  		}
			.duihuan{
				width: 60px;
				height: 22px;
				float: right;
				background: red;
				color: white;
				text-align: center;
				line-height: 20px;
				cursor: pointer;
				font-size: 15px;
				border-radius: 20%;
			}
			.dec{
				font-size: 15px;
				line-height: 60rpx;
			}
	  	}
</style>
